package com.zhanghe.study.springboot;

import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import java.io.IOException;

/**
 * @author zh
 * @date 2023/5/29 11:43
 */
public class TomcatServlet {
    public static void main(String[] args) throws LifecycleException {
        Tomcat tomcat = new Tomcat();
        Context context = tomcat.addContext("/test",null);
        Tomcat.addServlet(context, "helloServlet", new HttpServlet() {
            @Override
            public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
                res.getWriter().write("Hello World!!!");
            }
        });
        context.addServletMappingDecoded("/hello","helloServlet");
        tomcat.init();
        tomcat.start();
        // 阻塞主程序结束
        tomcat.getServer().await();
    }
}
